//
//  ViewController.m
//  IFlyUncaughtExceptions
//
//  Created by iflytek.com on 12-11-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "SendMailHandler.h"

@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];    
    
    _signalCallBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    _signalCallBtn.frame = CGRectMake(20, 10, 180, 50);
    [_signalCallBtn setTitle:@"Signal" forState:UIControlStateNormal];
    [_signalCallBtn addTarget:self action:@selector(onSignal) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_signalCallBtn];
    
    _checkAndSendMailBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    _checkAndSendMailBtn.frame = CGRectMake(20, 70, 180, 50);
    [_checkAndSendMailBtn setTitle:@"Check And Send Mail" forState:UIControlStateNormal];
    [_checkAndSendMailBtn addTarget:self action:@selector(onCheckAndSendMail) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_checkAndSendMailBtn];   
    
    _uncaughtCallBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    _uncaughtCallBtn.frame = CGRectMake(20, 140, 180, 50);
    [_uncaughtCallBtn setTitle:@"Uncaught" forState:UIControlStateNormal];
    [_uncaughtCallBtn addTarget:self action:@selector(onUncaught) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_uncaughtCallBtn];  
    
}

- (void)onSignal
{
    [self performSelector:@selector(string) withObject:nil afterDelay:0.0f];
}

- (void)badAccess
{
    void (*nullFunction)() = NULL;
    
    nullFunction();
}

- (void)onCheckAndSendMail
{
    SendMailHandler *handler = [[SendMailHandler alloc] init];
    
    handler.fromEmail = @"125345320@qq.com";
    handler.toEmail = @"125345320@qq.com";
    handler.relayHost = @"smtp.qq.com";
    handler.password = @"foreverloveyou";
    handler.subject = @"test message";
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    BOOL isSuccess = [handler addAttachmentsByDirPath:docPath];
    
    if(isSuccess)
    {
        [handler addMailContent:@"Test Mail Send Message"];
        [handler sendMail];
    }
}

- (void)onUncaught
{
    [self performSelector:@selector(badAccess) withObject:nil afterDelay:0.0f];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } 
    else 
    {
        return YES;
    }
}

@end
